#include "dynamicmapmanagewindow.h"
#include "dynamicmap.h"
#include "dynamicmapshowwindow.h"
#include <QDebug>

DynamicMapManageWindow::DynamicMapManageWindow(QObject *parent)
    : QObject(parent)
{
    show_widget = NULL;
#ifdef __linux__
    QDir dir(QDir::currentPath());
    run_path = QDir::currentPath();

    map = DynamicMap::get_map();
    map->init(run_path);

    m_res_path = map->get_res_path();

    m_img_path = QString("%1/%2").arg(m_res_path).arg(IMG_PATH);
    m_xml_path = QString("%1/%2").arg(m_res_path).arg(XML_PATH);

    connect(map, &DynamicMap::signal_update_file, this, &DynamicMapManageWindow::show_file);
#endif
}

DynamicMapManageWindow::~DynamicMapManageWindow()
{
    if (NULL != show_widget)
    {
        show_widget->hide();
        delete show_widget;
        show_widget = NULL;
    }
}

//如果是windows模拟，设置一下路径，，，linux还是按照老的方式自动获取map中的路径
void DynamicMapManageWindow::set_path(QString xml_path, QString img_path)
{
    m_xml_path = xml_path;
    m_img_path = img_path;
}

void DynamicMapManageWindow::print_simulate_data(ProtocolData *simulate_data)
{
    if (simulate_data == NULL)
        return;

    LOG_QDEBUG << "start_id:"        << simulate_data->start_id << " "
               << "stop_id:"         << simulate_data->stop_id << " "
               << "curr_or_next_id:" << simulate_data->curr_or_next_id << " "
               << "car_number:"      << simulate_data->car_number << " "
               << "door_status:"     << simulate_data->door_status << " "
               << "key_position:"    << simulate_data->key_position << " "
               << "ui_type:"         << simulate_data->ui_type << " "
               << "file_path:"       << simulate_data->file_path << " "
               << "route:"           << simulate_data->route << " "
               << "suffix:"          << simulate_data->suffix << " "
               << "scroll_msg:"      << simulate_data->scroll_msg << " "
               << "emergency_img:"   << simulate_data->emergency_img << "\n";

     LOG_QDEBUG << "skip_list:";
     foreach (int skipid, simulate_data->skip_list)
     {
         LOG_QDEBUG << skipid;
     }
     LOG_QDEBUG << "\n";

     LOG_QDEBUG << "items_change:";
     for (auto it = simulate_data->items_change.begin(); it != simulate_data->items_change.end(); it++)
     {
         LOG_QDEBUG << it.key() << ":" << (it.value()).content;
     }
     LOG_QDEBUG << "\n";
}

void DynamicMapManageWindow::reset_path(QString route)
{
    m_img_path = QString("%1/%2/%3").arg(m_res_path).arg(route).arg(IMG_PATH);
    m_xml_path = QString("%1/%2/%3").arg(m_res_path).arg(route).arg(XML_PATH);
}

void DynamicMapManageWindow::parse_path(QString &file_name, ProtocolData *data)
{
    if (file_name == "" || !data)
        return;

    if (!file_name.contains(".xml"))
        return;

    QString route = data->route;
    if (route != "")
    {
        QFileInfo info(m_res_path + QString("/%1").arg(route));
        if (info.isDir())
        {
            reset_path(route);
        }
    }
    LOG_QDEBUG << "img_path:" << m_img_path << "xml_path" << m_xml_path;
    return;
}

DiffShowType DynamicMapManageWindow::get_diffshow_type(ProtocolData *new_data)
{
/*
    *重新显示的类型*
    enum DiffShowType
    {
        ReShow = 0,          //全部重新显示
        DiffShowSuffix,      //差异显示语言切换的控件
        DiffShowItemsChange, //差异显示items_change
    };

    *协议结构*
    QString suffix;
    QString ui_type;
    QString file_path;
    QString route;
    int  start_id;
    int  stop_id;
    int  curr_or_next_id;
    int  car_number;
    int  door_status;
    int  key_position;
    QVector<int> skip_list;
    QVector<int> eta_list;
    QString scroll_msg;
    QString emergency_img;
    QVector<emergenry_list> emergency_text;
    QMap<QString, change_property> items_change;
*/
    LOG_QDEBUG << "print last data ";
    print_simulate_data(&m_last_data);

    if (new_data->suffix != m_last_data.suffix &&
        new_data->ui_type == m_last_data.ui_type &&
        new_data->file_path == m_last_data.file_path &&
        new_data->route == m_last_data.route &&
        new_data->start_id == m_last_data.start_id &&
        new_data->stop_id == m_last_data.stop_id &&
        new_data->curr_or_next_id == m_last_data.curr_or_next_id &&
        new_data->car_number == m_last_data.car_number &&
        new_data->door_status == m_last_data.door_status &&
        new_data->key_position == m_last_data.key_position &&
        new_data->scroll_msg == m_last_data.scroll_msg &&
        new_data->emergency_img == m_last_data.emergency_img &&
        //允许有越站和欲到站时间
        //new_data->skip_list.size() == 0 &&
        //new_data->eta_list.size() == 0 &&
        new_data->emergency_text.size() == 0 &&
        new_data->items_change.size() == 0)
    {
        LOG_QDEBUG << "\n\nrun DiffShowSuffix...";
        //只有后缀发生改变
        return DiffShowSuffix;

    } else if (new_data->suffix == m_last_data.suffix &&
               new_data->ui_type == m_last_data.ui_type &&
               new_data->file_path == m_last_data.file_path &&
               new_data->route == m_last_data.route &&
               new_data->start_id == m_last_data.start_id &&
               new_data->stop_id == m_last_data.stop_id &&
               new_data->curr_or_next_id == m_last_data.curr_or_next_id &&
               new_data->car_number == m_last_data.car_number &&
               new_data->door_status == m_last_data.door_status &&
               new_data->key_position == m_last_data.key_position &&
               new_data->scroll_msg == m_last_data.scroll_msg &&
               new_data->emergency_img == m_last_data.emergency_img &&
               //允许有欲到站时间
               //new_data->eta_list.size() == 0 &&
               new_data->emergency_text.size() == 0 &&
               new_data->items_change.size() != 0)
    {
        LOG_QDEBUG << "\n\nrun DiffShowItemsChange...";
        //只有items_change发生改变

        //越站列表必须要和上一包数据完全一样，才实行差异化显示
        if (new_data->skip_list.size() != m_last_data.skip_list.size())
            return ReShow;
        foreach (int skipid, new_data->skip_list)
           if (!m_last_data.skip_list.contains(skipid))
               return ReShow;

        for (auto it = new_data->items_change.begin(); it != new_data->items_change.end(); it++)
        {
            if (it.value().content.contains("##") ||
                it.key().compare("train") == ZERO ||
                it.key().contains("acrossxml"))
            {
                //此处不支持items_change动态传入样式的差异化显示,若有样式则一律重新显示
                //小火车控件itemschange不支持差异化显示，itemschange若有小火车则一律重新显示
                //修改跨xml控件一律重新显示，因为改文本需要重新加载样式
                LOG_QDEBUG << "\n\nrun DiffShowItemsChange...contains ## or train or acrossxml !! return ReShow";
                return ReShow;
            }
        }
        return DiffShowItemsChange;

    }
    return ReShow;
}

void DynamicMapManageWindow::record_last_data(ProtocolData *new_data)
{
    m_last_data.reset();
    m_last_data.suffix = new_data->suffix;
    m_last_data.ui_type = new_data->ui_type;
    m_last_data.file_path = new_data->file_path;
    m_last_data.route = new_data->route;
    m_last_data.start_id = new_data->start_id;
    m_last_data.stop_id = new_data->stop_id;
    m_last_data.curr_or_next_id = new_data->curr_or_next_id;
    m_last_data.car_number = new_data->car_number;
    m_last_data.door_status = new_data->door_status;
    m_last_data.key_position = new_data->key_position;
    m_last_data.scroll_msg = new_data->scroll_msg;
    m_last_data.emergency_img = new_data->emergency_img;
    m_last_data.skip_list = new_data->skip_list;
}

void DynamicMapManageWindow::show_file(QString file_name, void *param)
{
    if (file_name == "")
        return;
    LOG_QDEBUG << "\n\n=====================recv one package========================";

    //最终在simulate内部进行释放
    ProtocolData* python_data = static_cast<ProtocolData *>(param);

    //此处有内存泄漏，欢迎界面的传入NULL，python_data使用默认数据之后未删除
    if (python_data == NULL)
        python_data = new ProtocolData;
    print_simulate_data(python_data);

    parse_path(file_name, python_data);

    if (show_widget == NULL)
    {
        show_widget = new DynamicMapShowWindow(m_xml_path, m_img_path);
        show_widget->move(0, 1);
#ifdef __WIN32__
        show_widget->move(0, 150);
#endif
    }
    else
    {
        //每次更新前都会setmask清除抠掉区域
        show_widget->clearMask();

        //如果只是差异化显示则在此处处理，处理完之后直接返回
        DiffShowType dfstype = get_diffshow_type(python_data);//差异显示的类型
        if (dfstype != ReShow)
        {
            show_widget->diff_show_object(dfstype, python_data);
            record_last_data(python_data);
            return;
        }

        show_widget->hide_all_child();
    }


    //设置图名称和此幅图的数据,或者更新资源路径(由于更换了route)
    show_widget->set_show_data(file_name, python_data, m_xml_path, m_img_path);

    //此处show_file之前父窗口先show一下否则子控件grab的时候截屏截得不是最新的界面
    show_widget->show();

    //这里面单独show子控件，确保父控件已经show了，否则下面进行grab的时候会出问题
    show_widget->show_file(python_data);

    record_last_data(python_data);
}
